/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.services.applications;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.runtime.CoreException;
import org.zoundry.gaedt.core.util.ZGaeUtil;


/**
 * Class listens to changes to the the workspace, and makes changes accordingly.
 */
public class ZGaeApplicationResourceTracker implements IResourceChangeListener, IResourceDeltaVisitor
{
    /** The application service. */
    private ZGaeApplicationsService mApplicationsService;

    /**
     * @param aGaeApplicationsService
     */
    public ZGaeApplicationResourceTracker(ZGaeApplicationsService aGaeApplicationsService)
    {
        setApplicationsService(aGaeApplicationsService);
    }

    /**
     * Called when resources have changed we then may visit the delta provided
     * in the event.
     * 
     * @see org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent)
     */
    public void resourceChanged(IResourceChangeEvent aEvent)
    {
        try
        {
            IResourceDelta delta = aEvent.getDelta();
            if (delta != null)
                delta.accept(this);
        }
        catch (Exception e)
        {
            ZGaeUtil.logError(e);
        }
    }

    /**
     * Checks for project removal and processes indexes associated with the
     * project.
     * 
     * @see org.eclipse.core.resources.IResourceDeltaVisitor#visit(org.eclipse.core.resources.IResourceDelta)
     */
    public boolean visit(IResourceDelta aDelta) throws CoreException
    {
        // We are only interested in removed process projects.
        IResource resource = aDelta.getResource();
        if (resource instanceof IFile)
        {
            // We are only interested in GAE application resources.
            if (ZGaeApplicationUtil.isApplicationResource((IFile) resource))
            {
                if (aDelta.getKind() == IResourceDelta.REMOVED)
                {
                    mApplicationsService.onFileRemoved((IFile) resource);
                }
                else if (aDelta.getKind() == IResourceDelta.ADDED)
                {
                    mApplicationsService.onFileAdded((IFile) resource);
                }
                else if (aDelta.getKind() == IResourceDelta.CHANGED)
                {
                    if ((aDelta.getFlags() & IResourceDelta.MOVED_FROM) != 0)
                        mApplicationsService.onFileRemoved((IFile) resource);
                    else
                        mApplicationsService.onFileChanged((IFile) resource);
                }
                return false;
            }
        }
        return true;
    }

    /**
     * @return the applicationsService
     */
    protected ZGaeApplicationsService getApplicationsService()
    {
        return mApplicationsService;
    }

    /**
     * @param aApplicationsService the applicationsService to set
     */
    protected void setApplicationsService(ZGaeApplicationsService aApplicationsService)
    {
        mApplicationsService = aApplicationsService;
    }
}
